package com.tsystems.school;

import com.sun.org.apache.regexp.internal.REUtil;

/**
 * Created by IntelliJ IDEA.
 * User: victorn
 * Date: 8/14/11
 * Time: 12:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class MyLinkedList<K, V> {

    private ListItem<K, V> first;
    private ListItem<K, V> current;
    private ListItem<K, V> last;
    private int size;

    public MyLinkedList() {
        first = null;
        current = null;
        last = null;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public V put(K key, V val) {

        if (isEmpty()) {
            first = new ListItem(key, val, null);
            last = current = first;

            size++;

            return first.getValue();
        }

        ListItem<K,V> item = getListItem(key);


        if(item == null){
            item = new ListItem(key, val, null);

            last.setNext(item);

            last = item;
            size++;

            return last.getValue();

        } else {
            item.setValue(val);
            return item.getValue();
        }

    }

    public boolean containsKey(K key) {
        if (isEmpty())
            return false;

        ListItem<K, V> temp = current;

        //current = first;

        while (current != null) {
            if (current.getKey().equals(key))
                return true;

            current = current.getNext();
        }

        current = temp;

        temp = first;

        while (temp != current) {
            if (temp.getKey().equals(key)) {
                current = temp;
                return true;
            }

            temp = temp.getNext();
        }

        return false;
    }

    public boolean containsValue(V val) {
        if (isEmpty())
            return false;


        ListItem<K, V> temp = current;

        //current = first;

        while (current != null) {
            if (current.getValue().equals(val))
                return true;

            current = current.getNext();
        }

        current = temp;

        temp = first;

        while (temp != current) {
            if (temp.getValue().equals(val)) {
                current = temp;
                return true;
            }

            temp = temp.getNext();
        }

        return false;
    }

    public boolean contains(K key, V val) {
        if (isEmpty())
            return false;

        ListItem<K, V> temp = current;

        //current = first;

        while (current != null) {
            if (current.getValue().equals(val) && current.getKey().equals(key))
                return true;

            current = current.getNext();
        }

        current = temp;

        temp = first;

        while (temp != current) {
            if (temp.getValue().equals(val) && temp.getKey().equals(key)) {
                current = temp;
                return true;
            }

            temp = temp.getNext();
        }

        return false;
    }

    public V get(K key) {
        if (isEmpty())
            return null;

        ListItem<K, V> temp = current;

        //current = first;


        while (current != null) {
            if (current.getKey().equals(key))
                return current.getValue();

            current = current.getNext();
        }

        current = temp;

        temp = first;

        while (temp != current) {
            if (temp.getKey().equals(key)) {
                current = temp;
                return current.getValue();
            }

            temp = temp.getNext();
        }

        return null;
    }

    public V remove(K key) {

        if (isEmpty())
            return null;

        ListItem<K, V> prev = null;
        current = first;

        while (current != null) {
            if (current.getKey().equals(key)){

                if(prev != null){
                prev.setNext(current.getNext());
                prev = current;
                current = current.getNext();
                size--;
                return prev.getValue();
                }else{
                    size--;

                    prev = current;
                    current = first = current.getNext();

                    return prev.getValue();
                }
            }

            prev = current;
            current = current.getNext();
        }


        return null;
    }

    public ListItem<K, V> getListItem(K key) {
        if (isEmpty())
            return null;

        ListItem<K, V> temp = current;

        //current = first;

        while (current != null) {
            if (current.getKey().equals(key))
                return current;

            current = current.getNext();
        }

        current = temp;

        temp = first;

        while (temp != current) {
            if (temp.getKey().equals(key)) {
                current = temp;
                return current;
            }

            temp = temp.getNext();
        }

        return null;
    }

    public ListItem<K,V>[] asArray(){
        ListItem<K,V> arr[] = new ListItem[size];

        current = first;

        int i = 0;

        while(current != null){
            arr[i] = current;
            i++;
            current = current.getNext();
        }

        return arr;
    }


}
